<?php

/**
 * Model for notifications: by email etc
 * 
 * Corresponding method should determinte recipients list.
 *
 */
class Public_NotificationsModel extends BlogPublicBaseModel
{
    /**
     * Send notification about just added comment.
     *
     * @param Comment $comment
     */
    public function notificationNewComment(Comment $comment)
    {
        // first notify site owner
        $email = WereWordConfig::get('blog.owner_email');
        $msg_template = WereWordConfig::get('blog.notification_to_owner_template_body');
        $subject_template = WereWordConfig::get('blog.notification_to_owner_template_subject');
        
        $post = $comment->Post;
        
        $posts_model = $this->context->getModel('Posts', 'Public');
        $route_elements = $posts_model->getPostCoordinates($post);
        $post_url = $this->getContext()->getRouting()->gen('post.view', $route_elements, array('relative'=>FALSE));
        
        $msg = str_replace('%post_title%', $post->title, $msg_template);
        $msg = str_replace('%post_url%', $post_url, $msg);
        $msg = str_replace('%comment%', $comment->comment_text, $msg);
        $msg = str_replace('%approve_required%', $comment->is_approved ? 'No' : 'Yes', $msg);
        $msg = str_replace('%comment_url%', $post_url.'#c'.$comment->id, $msg);
        
        $subject = $subject_template;
        
        $this->sendEmail($subject, $msg, array($email), $email);
        // notify other recipients
        
        
    }
    
    /**
     * Send email(s).
     *
     * @param string $subject
     * @param string $text
     * @param array $recipients List of e-mail addresses
     * @param string $to Recipient that should be placed in the header "To:", if null make
     * 		list from the list of recipients
     */
    protected function sendEmail($subject, $text, $recipients, $to=null)
    {
        $from = sprintf("%s <%s>", WereWordConfig::get('blog.owner_name'), 
            WereWordConfig::get('blog.owner_email'));
        $headers = "From: $from\r\n";
        $headers .= "X-Mailer: WereWord/PHP\r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
        
        if (!is_null($to)) {
            $headers .= sprintf("Bcc: %s\r\n", implode(',', $recipients));
        } else {
            $to = implode(',', $recipients);
        }
        
        $text = base64_encode($text);
        // COMMENTED DUE TO SPAMMERS
        //mail($to, $subject, $text, $headers);
        // DEBUG do not send, just emulate
        //$mbox = '/home/cancel/projects/wereword/mbox';
        //$this->sendToMbox($mbox, $from, $to, $subject, $text, $headers);
    }
    
    protected function sendToMbox($filename, $from, $to, $subject, $text, $headers)
    {
        $f = fopen($filename, 'a');
        $data = sprintf("From %s %s\n", WereWordConfig::get('blog.owner_email'), date('D M j H:i:s Y'));
        $data .= "From: $from\r\n";
        $data .= "To: $to\r\n";
        $data .= "Subject: $subject\r\n";
        $data .= $headers;
        $data .= "\r\n" . $text;
        $data .= "\n\n";
        fwrite($f, $data);
        fclose($f);
    }
}

?>
